import React, {
    PureComponent
} from 'react';

import PropTypes from 'prop-types';

import {
    Actions
} from 'react-native-router-flux';

import Connect from './../../utils/connect';

import * as types from './../../state/types/user';

import userAction from './../../state/actions/user';

interface Props {
    user:any,
    active:boolean
    userActions:any
}

@Connect([`user`],{
    userActions:userAction
})
class Check extends PureComponent<Props> {
    static defaultProps = {
        user:{},
        active:false,
        userActions:{}
    }

    static childContextTypes = {
        onContextPress:PropTypes.func
    }

    componentDidMount() {
        const {
            active
        } = this.props;
        if(active) {
            this.autoCheck();
        }
    }

    autoCheck = async () => {
        await this.userActions[
            types.loginUser
        ]();
        this.onCheck();
    }

    get userActions() {
        const {
            userActions
        } = this.props;
        return userActions;
    }

    get userInfo() {
        const {
            user
        } = this.props;

        const {
            loginUser
        } = user;
        return loginUser || {};
    }

    get genderId() {
        const {
            gender
        } = this.userInfo;

        const {
            id = 0
        } = gender || {};

        return id;
    }

    get birthday() {
        const {
            birthday
        } = this.userInfo;
        return birthday
    }

    get location() {
        const {
            location
        } = this.userInfo;

        const {
            country
        } = location || {};

        const {
            id = 0
        } = country || {};
        return id;
    }

    get about() {
        const {
            aboutme = ``
        } = this.userInfo;
        return aboutme.toString().trim();
    }

    get photos() {
        const {
            photos = []
        } = this.userInfo;
        return photos.filter((item:any) => (
            item.attachId
        ))
    }

    getChildContext() {
        return {
            onContextPress:this.onCheck
        }
    }

    private jump = (initPage:number=0) => {
        Actions.push(`/user/register`,{
            isFull:true,
            initPage:initPage
        })
    }

    private onCheck = () => {
        if(!this.birthday || !this.genderId) {
            return this.jump(1);
        }

        if(!this.about) {
            return this.jump(3);
        }

        if(!this.photos.length) {
            return this.jump(4);
        }
    }

    render() {
        const {
            children
        } = this.props;
        return children || null;
    }
}

export default Check;